package com.example.borui.partpositionproject.partpisition.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.example.borui.partpositionproject.partpisition.base.BasePartPosition;
import com.example.borui.partpositionproject.partpisition.view.IPartPositionBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by borui on 2017/9/29.
 */
public abstract class BasePartPositionAdapter extends BaseRecyclerAdapter<Integer> implements IPartInterface<Integer>{

    private PartPistionAdapterHelper partPistionAdapterHelper;

    /**
     * 构造函数
     *
     * @param context
     */
    public BasePartPositionAdapter(Context context) {
        super(context);
        partPistionAdapterHelper = new PartPistionAdapterHelper(this);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return partPistionAdapterHelper.onCreateViewHolder(viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        partPistionAdapterHelper.onBindViewHolder(holder, position);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    @Override
    public Integer getItem(int location) {
        return super.getItem(location);
    }

    @Override
    public int getItemViewType(int position) {
        return partPistionAdapterHelper.getItemViewType(position);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return partPistionAdapterHelper.getSpanCount(position);
                }
            });
        }
    }

    @Override
    public abstract BasePartPosition buildProcessor(int partPosition, IPartPositionBean bean);


    public void updatePositionItems(List<IPartPositionBean> mDatas) {
        partPistionAdapterHelper.updatePositionItems(mDatas);
    }

    public void addPositionItem(IPartPositionBean mData) {
        partPistionAdapterHelper.addPositionItem(mData);
    }

    public void addPositionItems(List<IPartPositionBean> mDatas) {
        partPistionAdapterHelper.addPositionItems(mDatas);
    }

    public void notifyProcessAdapter() {
        partPistionAdapterHelper.notifyProcessAdapter();
    }

    public PartPistionAdapterHelper getPartPistionAdapterHelper() {
        return partPistionAdapterHelper;
    }

    @Override
    public void onDestory() {
        super.onDestory();
        if(null != getPartPistionAdapterHelper() && null != getPartPistionAdapterHelper().getHomeProcessList() &&
                getPartPistionAdapterHelper().getHomeProcessList().size() > 0){
            for(BasePartPosition basePartPosition:getPartPistionAdapterHelper().getHomeProcessList()){
                basePartPosition.onDestory();
            }
        }
    }
}
